#!/bin/bash

echo "=== 点云建图系统环境检查 (修正版) ==="
echo "当前目录: $(pwd)"
echo ""

# 检查ROS 2
if [ -z "$ROS_DISTRO" ]; then
    echo "❌ ROS 2环境未设置"
    echo "   请运行: source /opt/ros/humble/setup.bash"
else
    echo "✅ ROS 2: $ROS_DISTRO"
fi

echo ""

# 检查Python包
echo "Python环境检查:"
python3 -c "
import sys
print(f'Python路径: {sys.executable}')
print(f'Python版本: {sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}')

packages = [
    ('numpy', 'numpy'),
    ('rclpy', 'rclpy'),
    ('sensor_msgs_py', 'sensor_msgs_py.point_cloud2')
]

for name, import_name in packages:
    try:
        if '.' in import_name:
            module_name, attr_name = import_name.split('.')
            module = __import__(module_name, fromlist=[attr_name])
        else:
            module = __import__(import_name)
        if name == 'numpy':
            print(f'✅ {name}: {module.__version__}')
        else:
            print(f'✅ {name}: 可用')
    except ImportError as e:
        print(f'❌ {name}: 不可用 - {e}')
"

echo ""

# 使用绝对路径检查目录
echo "目录检查 (使用绝对路径):"
SHELL_DIR="$HOME/shell"
MAPS_DIR="$HOME/pointcloud_maps"

if [ -d "$SHELL_DIR" ]; then
    echo "✅ shell目录存在: $SHELL_DIR"
    if [ -f "$SHELL_DIR/save_pointcloud.py" ]; then
        echo "✅ 点云保存脚本存在"
    else
        echo "❌ 点云保存脚本不存在"
        echo "   请确保 save_pointcloud.py 在 $SHELL_DIR/"
    fi
else
    echo "❌ shell目录不存在: $SHELL_DIR"
    echo "   请创建目录: mkdir -p $SHELL_DIR"
fi

if [ -d "$MAPS_DIR" ]; then
    echo "✅ 点云保存目录存在: $MAPS_DIR"
else
    echo "⚠️  点云保存目录不存在，将在首次运行时创建"
fi

echo ""

# 检查RealSense相机
echo "硬件检查:"
if lsusb | grep -q "Intel Corp.*RealSense"; then
    echo "✅ RealSense相机已连接"
else
    echo "❌ RealSense相机未找到"
    echo "   请确保相机已连接到USB 3.0端口"
fi

echo ""
echo "=== 检查完成 ==="
